<template>
  <div class="upload-cover">
    <div class="cover-wrap">
      <img
        class="cover-image"
        ref="cover-image"
        :src="value"
        @click="showSelectCover"
      >
    </div>
    <el-dialog
      title="选择封面"
      :visible.sync="dialogVisible"
      append-to-body
    >
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="first">
          <!--
            ref 有两个作用：
            （1）作用到普通 html 标签上可以获取dom；
            （2）作用到组件上可以获取组件
          -->
          <image-list
            :is-show-add="false"
            :is-show-action="false"
            is-show-selected
            ref="images-list"
          />
        </el-tab-pane>
        <el-tab-pane label="上传图片" name="second">
          <div class="preview-image-wrap">
            <input type="file" ref="file" @change="onFileChange"/>
            <img class="preview-image" width="100" ref="preview-image">
          </div>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onCoverConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { uploadImage } from '@/api/image'
import ImageList from '@/views/image/components/image-list'

export default {
  name: 'UploadCover',
  components: {
    ImageList
  },
  props: ['value'],
  data () {
    return {
      dialogVisible: false, // 是否显示选择封面弹窗
      activeName: 'first' // 默认展示的选项卡
    }
  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {
  },
  methods: {
    showSelectCover () {
      this.dialogVisible = true
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]

      // 图片预览
      const blob = window.URL.createObjectURL(file)
      this.$refs['preview-image'].src = blob

      // 防止重复选择同一个图片不触发 change 事件
      // 但这里不需要这行代码，否则点击确定后拿不到文件
      // this.$refs.file.value = ''
    },
    onCoverConfirm () {
      if (this.activeName === 'second') {
        const file = this.$refs.file.files[0]
        if (!file) {
          this.$message('请选择文件')
          return
        }
        // 执行上传图片
        const fd = new FormData()
        fd.append('image', file)
        uploadImage(fd).then(res => {
          // 关闭弹出层
          this.dialogVisible = false
          this.$message({
            type: 'success',
            message: '文件上传成功'
          })
          // 展示上传的图片
          // this.$refs['cover-image'].src = res.data.data

          // 将选中的图片的url地址传给父组件
          this.$emit('input', res.data.data)
          // this.$emit('update-cover', res.data.data)
        })
      } else if (this.activeName === 'first') {
        // 另一种组件通信方式：父组件可以通过 ref 直接访问子组件中的数据
        const imageList = this.$refs['images-list']
        const selected = imageList.selected
        if (selected == null) {
          this.$message.error('请选择封面图片')
          return
        }
        // 关闭对话框
        this.dialogVisible = false
        // 修改父组件绑定数据
        this.$emit('input', imageList.images[selected].url)
      }
    }
  }
}
</script>

<style scoped lang="less">
.preview-image-wrap {
  display: flex;
  flex-direction: column;

  .preview-image {
    margin: 5px;
  }
}

.cover-wrap {
  .cover-image {
    background: url(./pic_bg.png) no-repeat;
    background-size: cover;
    width: 200px;
    height: 200px;
    margin: 5px;
  }
}
</style>
